package com.example.goandpaytthebill.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.goandpaytthebill.Bean.loginBean;
import com.example.goandpaytthebill.R;
import com.example.goandpaytthebill.ServerApi;
import com.example.goandpaytthebill.Tool.CountDownButton;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.Call;

public class ChangePasswordActivity extends AppCompatActivity {

    private EditText et_phone;
    private EditText et_yanzhengma;
    private EditText et_new_password;
    private CountDownButton tv_cx;
    private Button bt_loginin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //修改为深色，因为我们把状态栏的背景色修改为主题色白色，默认的文字及图标颜色为白色，导致看不到了。
            ChangePasswordActivity.this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        setView();
        initView();
    }

    private void initView() {
        bt_loginin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_phone.getText().toString().equals("")){
                    Toast.makeText(ChangePasswordActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                }else{
                    if (et_yanzhengma.getText().toString().equals("")){
                        Toast.makeText(ChangePasswordActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    }else {
                        if (et_new_password.getText().toString().equals("")){
                            Toast.makeText(ChangePasswordActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                        }else {
                            CZpassword();


                        }
                    }

                }
            }
        });
        tv_cx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_phone.getText().toString().equals("")){
                    Toast.makeText(ChangePasswordActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                }else{
                    sendEms();
                }
            }
        });
    }

    private void CZpassword() {
        OkHttpUtils
                .post()
                .url(ServerApi.resetpwd)
                .addParams("newpassword",et_new_password.getText().toString())
                .addParams("mobile",et_phone.getText().toString())
                .addParams("captcha",et_yanzhengma.getText().toString())

                .build()
                .execute(new StringCallback() {
                    private String msg;
                    private String code;

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            code = new JSONObject(response).getString("code");
                            msg = new JSONObject(response).getString("msg");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (code.equals("1")){
                            finish();
                        }
                        Toast.makeText(ChangePasswordActivity.this, msg, Toast.LENGTH_SHORT).show();


                    }
                });
    }


    private void sendEms() {
        OkHttpUtils
                .post()
                .url(ServerApi.sendsms)
                .addParams("mobile",et_phone.getText().toString())
                .addParams("event","resetpwd")
                .build()
                .execute(new StringCallback() {
                    private String msg;
                    private String code;

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            code = new JSONObject(response).getString("code");
                            msg = new JSONObject(response).getString("msg");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (code.equals("1")){
                            tv_cx.startCount();
                            Toast.makeText(ChangePasswordActivity.this, msg, Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(ChangePasswordActivity.this, msg, Toast.LENGTH_SHORT).show();
                        }

                    }
                });
    }
    private void setView() {
        et_phone=findViewById(R.id.et_phone);
        et_yanzhengma=findViewById(R.id.et_yanzhengma);
        et_new_password=findViewById(R.id.et_new_password);
        bt_loginin=findViewById(R.id.bt_loginin);
        tv_cx=findViewById(R.id.tv_cx);
    }
}